package com.ls.lserp.modules.custom.utils;

import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
import com.ls.lserp.common.mapper.JsonMapper;
import com.ls.lserp.common.utils.CacheUtils;
import com.ls.lserp.common.utils.Constants;
import com.ls.lserp.common.utils.SpringContextHolder;
import com.ls.lserp.common.utils.StringUtils;
import com.ls.lserp.modules.custom.dao.FitPropertyDao;
import  com.ls.lserp.modules.order.entity.FitProperty;
import java.util.List;
import java.util.Map;

/**
 * 量体工具类
 * @author LIUPENG
 * @version 2016-10-28
 */
public class FitPropertyUtils {
	
	private static FitPropertyDao fitPropertyDao = SpringContextHolder.getBean(FitPropertyDao.class);

	//code为键值
	public static final String CACHE_FITPROPERTY_MAP = "fitPropertyMap";
	//code-value为键值
	public static final String CACHE_FITPROPERTY_VALUE_MAP = "fitPropertyValueMap";
	
    
    /**
      * @Title: getFitPropertyList
      * @Description: 返回量体项目列表
      * @author LIUPENG
      * @date 2016-11-16 下午4:48:36
      *
      * @param code
      * @return
      *
      * @return List<FitProperty> 
      * @throws
      */
    
    public static List<FitProperty> getFitPropertyList(String code){
        
        @SuppressWarnings("unchecked")
        Map<String, List<FitProperty>> fitPropertyMap = (Map<String, List<FitProperty>>)CacheUtils.get(CACHE_FITPROPERTY_MAP);
        if (fitPropertyMap==null){
            fitPropertyMap = Maps.newHashMap();
            for (FitProperty fitProperty : fitPropertyDao.findAllList(new FitProperty())){
                List<FitProperty> fitPropertyList = fitPropertyMap.get(fitProperty.getItemCD());
                if (fitPropertyList != null){
                    fitPropertyList.add(fitProperty);
                }else{
                    fitPropertyMap.put(fitProperty.getPropertyCd(), Lists.newArrayList(fitProperty));
                }
            }
            CacheUtils.put(CACHE_FITPROPERTY_MAP, fitPropertyMap);
        }
        List<FitProperty> fitPropertyList = fitPropertyMap.get(code);
        
        if (fitPropertyList == null){
            fitPropertyList = Lists.newArrayList();
        }else{
            for (FitProperty fitProperty :fitPropertyList){
                if(Constants.EN_US.equals(StringUtils.getLanguage())){
                    fitProperty.setPropertyName(fitProperty.getPropertyNameEn());
                }else{
                    fitProperty.setPropertyName(fitProperty.getPropertyNameCn());
                }
            }
        }
        
        return fitPropertyList;
    }
    /**
     * @Title: getFitPropertyEntity
     * @Description: 返回量体Entity
     * @author liupeng
     * @date 2016-10-10 下午4:48:36
     *
     * @param code
     * @return
     *
     * @return FitProperty
     * @throws
     */
   
    public static FitProperty getFitPropertyEntity(String value){
        
        Map<String, FitProperty> fitPropertyValueMap = (Map<String, FitProperty>)CacheUtils.get(CACHE_FITPROPERTY_VALUE_MAP);
        
        if (fitPropertyValueMap==null){
            
            fitPropertyValueMap = Maps.newHashMap();
            for (FitProperty fitProperty : fitPropertyDao.findAllList(new FitProperty())){
                fitPropertyValueMap.put(fitProperty.getPropertyCd()+"-"+fitProperty.getPropertyValue(),fitProperty);
            }
            CacheUtils.put(CACHE_FITPROPERTY_VALUE_MAP, fitPropertyValueMap);
            
        }
        
        FitProperty returnFitProperty =fitPropertyValueMap.get(value);
        
        if(returnFitProperty!=null){
            return returnFitProperty;
        }else{
            return new FitProperty();
        }
         
    }
    /**
     * 返回数据量体名称
     * @param value
     * @return
     */
    public static String getFitPropertyName(String value){
        
        Map<String, FitProperty> fitPropertyValueMap = (Map<String, FitProperty>)CacheUtils.get(CACHE_FITPROPERTY_VALUE_MAP);
        
        if (fitPropertyValueMap==null){
            
            fitPropertyValueMap = Maps.newHashMap();
            for (FitProperty fitProperty : fitPropertyDao.findAllList(new FitProperty())){
                fitPropertyValueMap.put(fitProperty.getPropertyCd()+"-"+fitProperty.getPropertyValue(),fitProperty);
            }
            CacheUtils.put(CACHE_FITPROPERTY_VALUE_MAP, fitPropertyValueMap);
            
        }
        
        FitProperty returnFitProperty =fitPropertyValueMap.get(value);
        
        if(returnFitProperty!=null){
            if(Constants.EN_US.equals(StringUtils.getLanguage())){
                return returnFitProperty.getPropertyNameEn();
            }else{
                return returnFitProperty.getPropertyNameCn();
            }
        }else{
            return "";
        }
    }

    /**
      * @Title: getFitPropertyListJson
      * @Description: 返回量体列表（JSON）
      * @author LiuPeng 
      * @date 2016-11-22 下午4:48:10
      *
      * @param code
      * @return
      *
      * @return String 
      * @throws
      */
    
    /**
     * 返回量体列表（JSON）
     * @param type
     * @return
     */
    public static String getFitPropertyListJson(String code){
        return JsonMapper.toJsonString(getFitPropertyList(code));
    }
	

}
